package com.example.leetdemo.plan1;

import java.util.Arrays;

public class Sword039 {

    public int largestRectangleArea(int[] heights) {
        if (heights.length == 0) {
            return 0;
        }
        int[] sq = new int[heights.length];
        for (int i = 0; i < heights.length; i++) {
            if (i != 0 && heights[i] == heights[i - 1]) {
                sq[i] = sq[i - 1];
            } else {
                sq[i] = calculator(i, heights) * heights[i];
            }
        }
        Arrays.sort(sq);
        return sq[heights.length - 1];
    }

    private int calculator(int i, int[] heights) {
        int q = i - 1, p = i + 1;
        while (q >= 0) {
            if (heights[q] < heights[i]) {
                break;
            }
            q--;
        }
        while (p < heights.length) {
            if (heights[p] < heights[i]) {
                break;
            }
            p++;
        }
        return p - q - 1 == 0 ? 1 : p - q - 1;
    }

    public static void main(String[] args) {
        Sword039 sword039 = new Sword039();
        System.err.println(sword039.largestRectangleArea(new int[] { 2,1,2}));
    }


    public int largestRectangleArea1(int[] heights) {

        return 0;
    }
}
